<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="../../res/layui/css/layui.css" media="all">
    <link rel="stylesheet"
          href="res/zTree_v3-master/css/metroStyle/metroGreen.css"
          type="text/css">
    <script src="../../res/js/jquery-3.4.1.min.js" type="text/javascript"
            charset="utf-8"></script>
    <script type="text/javascript" src="../../res/layui/layui.js"></script>
    <script type="text/javascript" src="../../res/js/base.js"></script>
    <script type="text/javascript">

        //全局同步
        $.ajaxSetup({
            async: false
        });
    </script>
    <title>操作界面</title>
</head>
<body>
<div style="margin-top: 10px; padding-right: 27px;">
    <form class="layui-form" id="idform" action="" method="post"

          enctype="multipart/form-data">
        <input type="hidden" id="trueUserNum" name="trueUserNum">
        <div class="layui-form-item">
            <label class="layui-form-label">职业</label>
            <div class="layui-input-block">
                <select id="zhiye" name="company" lay-filter="aihao" lay-verify="required">
                    <option value=""></option>
                    <option value="1">领导</option>
                    <option value="2">教练员</option>
                    <option value="3">专家</option>
                    <option value="4">运动员</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">真实用户</label>
            <div class="layui-input-block">
                <select id="trueUser" name="trueUser" lay-filter="trueUser" lay-verify="required" lay-search>
                    <option value=""></option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">身份证</label>
            <div class="layui-input-block">
                <input type="text" id="userEmail" name="userEmail" lay-verify="required|title"
                       autocomplete="off" placeholder="身份证" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-block">
                <input type="text" id="userName" name="userName" lay-verify="required|title"
                       autocomplete="off" placeholder="用户名" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">密码</label>
            <div class="layui-input-block">
                <input type="password" id="userPassword" name="userPassword" lay-verify="required|title"
                       autocomplete="off" placeholder="密码" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">手机</label>
            <div class="layui-input-block">
                <input type="text" id="userPhone" name="userPhone" lay-verify="phone"
                       autocomplete="off" placeholder="手机" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">照片</label>
            <div class="layui-input-block">

                <input type="file" name="userImgae"
                       autocomplete="off" class="layui-input">
            </div>
        </div>


        <div class="layui-form-item">
            <label class="layui-form-label">复选框</label>
            <div id="checkBox1" class="layui-input-block">

            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="submit" class="layui-btn" lay-submit
                        lay-filter="demo1">立即提交
                </button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>
<script>
    $(function () {
        //复选框赋值
        var url = "../../../user/selectAll";
        var data = null;
        $.post(url, data, function (obj) {
            $.each(obj, function (index, item) {
                $("#checkBox1").append("<input type='checkbox' id='" + item.chaId + "' name='juese' title='" + item.chaName + "' value='" + item.chaId + "'>");
            })
        }, "json")
    })
    //Demo
    layui.use(['form','layer'], function () {
        var form = layui.form;
        var layer = layui.layer;
        //下拉列表出现值
        form.on('select(aihao)', function (data) {
            var url = "";
            if (data.value == 1) {//领导
                url = getRootPath() + "AtsEmpLeaderController/findAll";
            } else if (data.value == 2) {//教练员
                url = getRootPath() + "AtsEmpCoachController/findAll";
            } else if (data.value == 3) {//专家
                url = getRootPath() + "AtsEmpExpertController/findAll";
            } else if (data.value == 4) {//运动员
                url = getRootPath() + "AtsEmpAthleteController/findAll";
            } else if (data.value == '') {
                $("#trueUser").find("option").remove();
                form.render('select');
            }
            var data1 = null;
            $.post(url, data1, function (obj) {
                $("#trueUser").html("<option value=\"\"></option>");
                if (obj != null) {
                    $.each(obj, function (index, item) {
                        if (data.value == 1) {//领导
                            if (item.remark == "N") {
                                $("#trueUser").append('<option value=' + item.leaderId + ' disabled>' + item.leaderName + '</option>');
                            } else {
                                $("#trueUser").append('<option value=' + item.leaderId + '>' + item.leaderName + '</option>');
                            }
                        } else if (data.value == 2) {//教练员
                            if (item.remark == "N") {
                                $("#trueUser").append('<option value=' + item.coachId + ' disabled>' + item.coachName + '</option>');
                            } else {
                                $("#trueUser").append('<option value=' + item.coachId + '>' + item.coachName + '</option>');
                            }
                        } else if (data.value == 3) {//专家
                            if (item.remark == "N") {
                                $("#trueUser").append('<option value=' + item.expertId + ' disabled>' + item.expertName + '</option>');
                            } else {
                                $("#trueUser").append('<option value=' + item.expertId + '>' + item.expertName + '</option>');
                            }
                        } else if (data.value == 4) {//运动员
                            if (item.remark == "N") {
                                $("#trueUser").append('<option value=' + item.athleteId + ' disabled>' + item.athleteName + '</option>');
                            } else {
                                $("#trueUser").append('<option value=' + item.athleteId + '>' + item.athleteName + '</option>');
                            }
                        }
                    });
                }

                form.render();
            }, "json")
        })

        selectOne(form);

        //自定义验证规则
        /* form.verify({
             title: function(value){
                 if(value.length < 5){
                     return '标题至少得5个字符啊';
                 }
             }
             ,phone: [
                 /^1[3456789]\d{9}$/
                 ,'电话,岂能填错！'
             ]
         });*/
        //监听提交
        form.on('submit(demo1)', function (data) {
            var index1 = layer.load(1); //添加laoding,0-2两种方式
            var form = new FormData(document.getElementById("idform"));
            form.userId = getQueryStringOne("userId");
            $.ajax({
                url: "../../../user/addOrup?trueUserText=" + $("#trueUser option:selected").text() + "&zhiyeValue=" + $("#zhiye").val() + "&zhiyeText=" + $("#zhiye option:selected").text() + "&userId=" + getQueryStringOne("userId") + "",
                type: "post",
                data: form,
                dataType: "json",
                processData: false,
                contentType: false,
                success: function (obj) {
                    layer.close(index1);    //返回数据关闭loading
                    if (obj.status == 1) {
                        //弹出需求
                        layer.msg(obj.msg, {
                            icon: 1,
                            time: 1000
                            //一秒关闭 如果不配置是3秒
                        }, function () {
                            //关闭 层
                            var index = parent.layer
                                .getFrameIndex(window.name);//得到iframe 层的索引
                            parent.layui.table.reload('testReload');//重载父页表格，参数为表格ID
                            parent.layer.close(index);//关闭改层
                            //刷新主界面
                            //parent.location.reload();
                        });
                    } else {
                        layer.msg(obj.msg);
                    }
                },
                error: function (e) {

                }
            });
            return false;
        });
    });


    function selectOne(form) {
        //修改是出现的值
        var one = getQueryStringOne("userId");
        if (one != "") {
            var url = "../../../user/selectOne";
            var data = {userId: getQueryStringOne("userId")};
            $.post(url, data, function (obj) {
                $("#userEmail").val(obj.vo.userEmail);
                $("#userPhone").val(obj.vo.userPhone);
                $("#userPassword").val(obj.vo.userPassword);
                $("#userName").val(obj.vo.userName);
                $("#zhiye").val(obj.vo.companyNum);
                $("#zhiye").siblings("div.layui-form-select").find('dl dd[lay-value=' + obj.vo.companyNum + ']').click();
                //selectTwo(obj.vo.companyNum,obj.vo.filId);
                $("#trueUser").val(obj.vo.filId);
                $("#trueUserNum").val(obj.vo.filId);
                $.each(obj.list, function (index, item) {
                    $("#" + item.chaId + "").prop("checked", true);
                })
                form.render();

            }, 'json')
        }
    }

    //得到地址栏上的参数  可以 是中文
    function getQueryStringOne(key) {
        var reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null)
            return decodeURI(r[2]);
        return null;
    }
</script>
</body>
</html>